//
//  UICollectionView+Extension.swift
//  YangFoundation
//
//  Created by yuyang on 2021/12/21.
//

import UIKit

// MARK: - UICollectionView register and dequeue reuse
public extension UICollectionView {

    /// UICollectionView cell register
    ///
    /// - Parameters:
    ///   - cellClass: UICollectionViewCell class
    func register<T: UICollectionViewCell>(_ cellClass: T.Type) {
        let name = String(describing: cellClass)
        self.register(cellClass, forCellWithReuseIdentifier: name)
    }

    /// UICollectionView reusable cell dequeue
    ///
    /// - Parameters:
    ///   - cellClass: UICollectionViewCell class
    ///   - indexPath: cell indexPath
    /// - Return: cell
    func dequeueReusableCell<T: UICollectionViewCell>(withCellClass cellClass: T.Type, for indexPath: IndexPath) -> T {
        let name = String(describing: cellClass)
        let cell = dequeueReusableCell(withReuseIdentifier: name, for: indexPath) as! T
        return cell
    }

    /// UICollectionView headerView register
    ///
    /// - Parameters:
    ///   - viewClass: UICollectionReusableView class
    func registerHeaderViewClass<T: UICollectionReusableView>(_ viewClass: T.Type) {
        let name = String(describing: viewClass)
        self.register(viewClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: name)
    }

    /// UICollectionView reusable headerView dequeue
    ///
    /// - Parameters:
    ///   - viewClass: UICollectionReusableView class
    ///   - indexPath: headerView indexPath
    /// - Return: headerView
    func dequeueReusableHeaderView<T: UICollectionReusableView>(withViewClass viewClass: T.Type, for indexPath: IndexPath) -> T {
        let name = String(describing: viewClass)
        let view = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: name, for: indexPath) as! T
        return view
    }

    /// UICollectionView footerView register
    ///
    /// - Parameters:
    ///   - viewClass: UICollectionReusableView class
    func registerFooterViewClass<T: UICollectionReusableView>(_ viewClass: T.Type) {
        let name = String(describing: viewClass)
        self.register(viewClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: name)
    }

    /// UICollectionView reusable footerView dequeue
    ///
    /// - Parameters:
    ///   - viewClass: UICollectionReusableView class
    ///   - indexPath: footerView indexPath
    /// - Return: footerView
    func dequeueReusableFooterView<T: UICollectionReusableView>(withViewClass viewClass: T.Type, for indexPath: IndexPath) -> T {
        let name = String(describing: viewClass)
        let view = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: name, for: indexPath) as! T
        return view
    }
}
